package com.privatetime.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.privatetime.model.User;

public class LoginAction extends BaseAction implements ActionStateConstants,
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private String userName;

	private String passWord;

	private boolean rememberMe;

	@SkipValidation
	public String jumpLogin() throws Exception {
		User loginUser = (User) getSession().get(SESSION_USER);
		if (null != loginUser) {
			return jumpMain();
		}
		return ActionStateConstants.LOGIN;
	}

	@SkipValidation
	public String jumpMain() throws Exception {
		return ActionStateConstants.MAIN;
	}

	public String login() throws Exception {
		logger.info("username=" + getUserName() + " password=" + getPassWord());
		User loginUser = userService.userLogin(getUserName(), getPassWord());
		logger.info("Login User is " + loginUser);
		if (loginUser != null) {
			logger.info("Cookie Enable=" + rememberMe);
			if (rememberMe) {
				Cookie cookie = new Cookie("privatetime", userName + "=="
						+ passWord);
				cookie.setMaxAge(60 * 60 * 24 * 30);
				response.addCookie(cookie);
			}
			getSession().put(SESSION_USER, loginUser);
			return ActionStateConstants.SUCCESS;
		} else {
			addFieldError("loginFailure", "Username or Password is not correct");
			return ActionStateConstants.INPUT;
		}
	}

	@SkipValidation
	// if no need to validate, please add the annotation.
	public String logout() throws Exception {
		if (getSession().containsKey(SESSION_USER)) {
			logger.info("User '" + getSession().get(SESSION_USER)
					+ "' will logout");
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("privatetime")) {
						cookie.setValue("");
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
			getSession().remove(SESSION_USER);
		}
		logger.info("User '" + getSession().get(SESSION_USER));
		return ActionStateConstants.SUCCESS;
	}

	@Override
	public void validate() {
		if (null == getUserName() || "".equals(getUserName())) {
			addFieldError("username", "Username is required");
		}
		if (null == getPassWord() || "".equals(getPassWord())) {
			addFieldError("password", "Password is required");
		}
	}

	@Override
	public String create() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String retrieve() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public boolean isRememberMe() {
		return rememberMe;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
